{extend name="base"/}
{block name="resources"}
<style type="text/css">
	.ns-form {margin-top: 0;}
	.tree-line{padding:10px 0;background:#ededed;margin-bottom:2px;line-height: 1.8;}
	.tree-line .layui-form{padding-left: 10px !important;}
	.tree-line .layui-form-checkbox{margin:0 10px !important;vertical-align:middle;}
	.ns-form {margin-top: 0;}
	.ns-item-block-hover-a {cursor: pointer;}
	.ns-input-box {position: absolute; right: 0; top: 50%; transform: translateY(-50%);}
	.layui-form-item .layui-form-checkbox[lay-skin=primary] {margin-top: 0;}
</style>
{/block}
{block name="main"}
<div class="layui-form ns-form">
	
	<div class="layui-form-item">
		<label class="layui-form-label"><span class="required">*</span>短信套餐名称：</label>
		<div class="layui-input-block">
			<input name="sms_name" type="text" value = "{$info.sms_name}" required lay-verify="required" class="layui-input ns-len-long">
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label img-upload-lable">封面：</label>
		<div class="layui-input-block img-upload">
			<div class="upload-img-block icon">
				<div class="upload-img-box {if !empty($info.sms_image)}hover{/if}">
					<div class="ns-upload-default" id="sms_image">
						{if empty($info.image)}
						<div class="upload">
							<img src="__STATIC__/img/upload_img.png" />
							<p>点击上传</p>
						</div>
						{else/}
						<div id="preview_sms_image" class="preview_img">
							<img layer-src src="{:img($info.sms_image)}" class="img_prev"/>
						</div>
						{/if}
					</div>
					<div class="operation"  >
						<div >
							<i title="图片预览" class="iconfont iconreview js-preview" style="margin-right: 20px;"></i>
							<i title="删除图片" class="layui-icon layui-icon-delete js-delete"></i>
						</div>
						<div class="replace_img js-replace">点击替换</div>
					</div>
					<input type="hidden" name="sms_image" value="{$info.sms_image}" />
				</div>
			</div>
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label"><span class="required">*</span>短信条数：</label>
		<div class="layui-input-block">
			<input name="sms_num" type="number" required lay-verify="required|number|int" value="{$info.sms_num}" class="layui-input ns-len-short"></input>
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label"><span class="required">*</span>套餐价格：</label>
		<div class="layui-input-inline">
			<input name="sms_money" type="number" required lay-verify="required|number|flnum" value="{$info.sms_money}" class="layui-input ns-len-short"/>
		</div>
		<div class="layui-form-mid layui-word-aux">元</div>
	</div>
	
	<div class="layui-form-item distribution-index">
		<label class="layui-form-label">是否上架：</label>
		<div class="layui-input-block">
			<div class="layui-input-inline">
				<input type="radio" name="status" value="1" lay-filter="level" title="上架" {if $info.status == 1} checked {/if} />
				<input type="radio" name="status" value="0" lay-filter="level" title="下架" {if $info.status == 0} checked {/if}/>
			</div>
		</div>
	</div>
	
	<div class="layui-form-item">
		<label class="layui-form-label">描述：</label>
		<div class="layui-input-block">
			<textarea name="desc" class="layui-textarea ns-len-long">{$info.desc}</textarea>
		</div>
	</div>
	
	<!-- 表单操作 -->
	<div class="ns-form-row">
		<button class="layui-btn ns-bg-color" lay-submit lay-filter="save">保存</button>
		<button class="layui-btn layui-btn-primary" onclick="back()">返回</button>
	</div>
</div>

{/block}
{block name="script"}
<script>
	layui.use(['form'], function() {
		var form = layui.form,
			repeat_flag = false;//防重复标识
		form.render();

		var upload = new Upload({
			elem: '#sms_image',
			post:"admin"
		});

        form.on('submit(save)', function (data) {

			// 删除图片
			if(!data.field.sms_image) upload.delete();

            if (repeat_flag) return;
            repeat_flag = true;
			
            $.ajax({
                type: "POST",
                dataType: "JSON",
                url: ns.url("admin/syssmspackage/edit", {package_id: '{$info.package_id}'}),
                data: data.field,
                success: function (res) {
					repeat_flag = false;
					if (res.code == 0) {
						layer.confirm('编辑成功', {
							title:'操作提示',
							btn: ['返回列表', '继续操作'],
							yes: function(){
								location.href = ns.url("admin/syssmspackage/lists")
							},
							btn2: function() {
								location.reload()
							}
						})
					}else{
						layer.msg(res.message);
					}
                }
            });
        });

		form.verify({
			flnum: function (value, item) {
				var str = $(item).parents(".layui-form-item").find("label").text().split("*").join("");
				str = str.substring(0, str.length - 1);
				
				var arrMen = value.split(".");
				var val = 0;
				if (arrMen.length == 2) {
					val = arrMen[1];
				}
				if (val.length > 2) {
					return str + "最多可保留两位小数";
				}
			},
			number: function (value, item) {
				var str = $(item).parents(".layui-form-item").find("label").text().split("*").join("");
				str = str.substring(0, str.length - 1);
				
				if (value < 0) {
					return str + "不能小于0";
				}
			},
			int: function (value, item) {
				var str = $(item).parents(".layui-form-item").find("label").text().split("*").join("");
				str = str.substring(0, str.length - 1);
				
				if (value%1 != 0) {
					return str + "必须为整数";
				}
			}
		});
    });
	
	function back() {
		location.href = ns.url("admin/syssmspackage/lists");
	}

</script>
{/block}